package icu.wdtt.controller;

import icu.wdtt.Utils.ResultUtils;
import icu.wdtt.enumModel.HttpRequestMessage;
import icu.wdtt.model.Result;
import icu.wdtt.model.Role;
import icu.wdtt.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("role")
@ApiModel(value = "角色模块",description = "用于定义某个用的角色，新注册用户都为普通用户，需要管理员权限才可访问")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private ResultUtils resultUtils;

    @GetMapping("getAll")
    public Result getAll(){

            List<Role> roleList = roleService.getAll();
            if(roleList != null){
                return resultUtils.success(HttpRequestMessage.SUCCESS_QUERY,roleList,roleList.size());
            }
        return resultUtils.fail(HttpRequestMessage.FAIL_QUERY);
    }

    @GetMapping(path = "get",params = "rid")
    public Result get(int rid){

            Role role = roleService.get(rid);
            if(role != null){
                return resultUtils.success(HttpRequestMessage.SUCCESS_QUERY,role);
            }

        return resultUtils.fail(HttpRequestMessage.FAIL_QUERY);
    }
    @PostMapping("add")
    public Result  add(Role role){

            Boolean flag = roleService.add(role);
            if(flag)return resultUtils.success(HttpRequestMessage.SUCCESS_ADD);

        return resultUtils.fail(HttpRequestMessage.FAIL_ADD);
    }
    @PostMapping(path = "del",params = "rid")
    public Result  del(int rid){

            Boolean flag = roleService.del(rid);
            if(flag)return resultUtils.success(HttpRequestMessage.SUCCESS_DELETE);

        return resultUtils.fail(HttpRequestMessage.FAIL_DELETE);
    }
    @PostMapping(path = "update",params = "rid")
    public Result  update(Role role){

            Boolean flag = roleService.update(role);
            if(flag)return resultUtils.success(HttpRequestMessage.SUCCESS_UPDATE);
        return resultUtils.fail(HttpRequestMessage.FAIL_UPDATE);
    }

}
